import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution890 {
    public List<String> findAndReplacePattern(String[] words, String pattern) {
        List<String> ans=new ArrayList<>();
        for (String word : words) {
            if (similar(word,pattern)){
                ans.add(word);
            }
        }
        return ans;
    }

    private boolean similar(String word, String pattern) {
        if (word.length()!=pattern.length()){
            return false;
        }
        Map<Character,Character> map1=new HashMap<>();
        Map<Character,Character> map2=new HashMap<>();
        for (int i=0;i<word.length();i++){
            if (!map1.containsKey(word.charAt(i)) && !map2.containsKey(pattern.charAt(i))){
                map1.put(word.charAt(i),pattern.charAt(i));
                map2.put(pattern.charAt(i),word.charAt(i));
            }else if (map1.containsKey(word.charAt(i)) && map2.containsKey(pattern.charAt(i))){
                if (map1.get(word.charAt(i))!=pattern.charAt(i) || map2.get(pattern.charAt(i))!=word.charAt(i)){
                    return false;
                }
            }else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(new Solution890().findAndReplacePattern(new String[]{"abc","deq","mee","aqq","dkd","ccc"},"abb"));
    }
}
